﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SYS.Web.Models;
using HtmlAgilityPack;
using System.Text;
using SYS.Common;
using System.Net;
namespace SYS.Web.Function
{
    public class GetData
    {
        public static bool FromTBByKeyword(string keyword, int pageNo, List<TBItem> items, List<string> suggestData)
        {
            string pageParam = "&s=" + (pageNo - 1) * 40 + "&style=grid";

            HtmlWeb web = new HtmlWeb();
            web.UseCookies = false;
            web.NeedParse = false;
            web.OverrideEncoding = Encoding.GetEncoding("gb2312");
            web.PreRequest += req;
            //&sort=sale-desc&tab=mall&
            pageParam += "&sort=sale-desc";
            keyword = keyword.UrlEncode();
            //pageParam = "&sort=sale-desc&commend=all&search_type=item&sourceId=tb.index&spm=1.1000386.5803581.d4908513&initiative_id=tbindexz_20130703";
            //http://s8.taobao.com/search?q=%C5%AE%D7%B0&commend=all&search_type=item&sourceId=tb.index&spm=1.1000386.5803581.d4908513&initiative_id=tbindexz_20130703
            HtmlDocument hd = web.Load("http://s8.taobao.com/search?q=" + keyword + pageParam);
            string result = hd.Text;
            RegexModel m = new RegexModel();
            m.OriginText = result;
            Common.Log.txtLog( result);
            m.Pattern = "id=\"list-content\".*?<div\\sclass=\"col-sub\">";
            m.SonPattern = "class=\"list-item\"(.*?)class=\"legend2\"";
            Dictionary<string, string> itemPattern = new Dictionary<string, string>();
            itemPattern.Add("itemid", "data-item=\"(?<itemid>.*?)\"");
            itemPattern.Add("name", "title=\"(?<name>.*?)\"");
            itemPattern.Add("imgurl", "lazyload=\"(?<imgurl>.*?)\"");
            //itemPattern.Add("comment", "");
            itemPattern.Add("seller", "class=\"seller\"><a.*?>(?<seller>.*?)</a>");
            itemPattern.Add("loc", "class=\"loc\">(?<loc>.*?)</span>");
            itemPattern.Add("price", "class=\"price\"><em>(?<price>.*?)</em>");
            itemPattern.Add("dealing", "</em>\\s<span >(?<dealing>.*?)</span>");
            itemPattern.Add("fee", "class=\"fee\">(?<fee>.*?)</span>");
            m.ItemPattern = itemPattern;
            var rs = m.GetAllItems();
            if (rs.Count > 0)
            {
                for (int i = 0; i < rs.Count; i++)
                {

                    if (!rs[i].ContainsKey("itemid"))
                    {
                        continue;
                    }
                    TBItem item = new TBItem();
                    item.comment = rs[i].ContainsKey("comment") ? rs[i]["comment"][0] : "";
                    item.dealing = rs[i].ContainsKey("dealing") ? rs[i]["dealing"][0] : "";
                    item.fee = rs[i].ContainsKey("fee") ? rs[i]["fee"][0].RemoveHTML() : "";
                    item.imgurl = rs[i].ContainsKey("imgurl") ? rs[i]["imgurl"][0] : "";
                    item.ItemId = rs[i]["itemid"][0];
                    item.loc = rs[i].ContainsKey("loc") ? rs[i]["loc"][0] : "";
                    item.name = rs[i].ContainsKey("name") ? rs[i]["name"][0] : "";
                    item.newprice = "";
                    item.price = rs[i].ContainsKey("price") ? rs[i]["price"][0] : "";
                    item.seller = rs[i].ContainsKey("seller") ? rs[i]["seller"][0] : "";
                    item.url = "";
                    items.Add(item);
                }
            }
            m.SonPattern = "class=\"related-search\"(.*?)</div>";
            m.Pattern = "";
            itemPattern.Clear();
            itemPattern.Add("suggest", "<a.*?>(?<suggest>.*?)</a>");
            m.ItemPattern = itemPattern;
            rs = m.GetAllItems();
            if (rs.Count > 0)
            {
                for (int i = 0; i < rs.Count; i++)
                {
                    foreach (var item in rs[i].Keys)
                    {
                        for (int j = 0; j < rs[i][item].Length; j++)
                        {
                            suggestData.Add(rs[i][item][j]);
                        }

                    }
                }
            }
            return true;
        }
        private static bool req(System.Net.HttpWebRequest request)
        {
            request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8");
            request.Headers.Add("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
            request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
            request.Headers.Add("Origin", "http://search.taobao.com");
            request.AutomaticDecompression = DecompressionMethods.GZip;
            request.Host = "search.taobao.com";
            request.Referer = "http://search.taobao.com/search?q=%B4%F3%C5%C6%D7%D3&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=1.1000386.5803581.d4908513";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31";
            return true;
        }

    }
}